package yxy.game.pm2.command.friend;

import yxy.cherry.data.bean.friendsdate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FriendMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.friend.FriendModule;
import yxy.game.pm2.module.task.TaskHandle;

import java.util.Set;

/** 好友 一件赠送 */
@MessageType(proto = Command.FriendPointGive, classtype = FriendMessage.FriendPointGiveRequest.class)
public class CMD_FriendPointGive extends Command<FriendMessage.FriendPointGiveRequest> {

	@Override
	public void execute(MessageSource messageSource, FriendMessage.FriendPointGiveRequest request) {
		String playerCode = messageSource.getTag();

		FriendMessage.FriendPointGiveResponse.Builder response = FriendMessage.FriendPointGiveResponse.newBuilder();

		FriendModule playerModule = FriendModule.get(playerCode);
		friendsdate data = playerModule.data();
		// 赠送上限
		if (playerModule.getGiven().size() >= data.getUplimit()) {
			send(messageSource, response, Result.FriendPointNoTimes);// 你今天的赠送次数已达上限
			return;
		}
		CurrencyHandle currencyHandle = new CurrencyHandle();
		Set<String> all = playerModule.getFriend();
		try {
			if (playerModule.tryLock()) {
				int num = 0;

				for (String friendCode : all) {
					// 赠送上限
					if (playerModule.getGiven().size() >= data.getUplimit()) {
						// 赠送已达上限
						break;
					}
					if (!playerModule.getFriend().contains(friendCode)) {
						continue;
					}
					if (playerModule.getGiven().contains(friendCode)) {
						continue;
					}
					FriendModule friendModule = FriendModule.get(friendCode);
					try {
						if (friendModule.tryLock()) {
							playerModule.getGiven().add(friendCode);
							friendModule.getGiftn().add(playerCode);

							playerModule.set();
							friendModule.set();

							num++;
						}
					} finally {
						friendModule.unlock();
					}
				}
				if (num <= 0) {
					send(messageSource, response, Result.Success);
					return;
				}
				Currency reward = Currency.create(data.getGivegettype(), data.getGivegetid(), data.getGivegetnum() * num);
				currencyHandle.addCurrency(playerCode, CCReason.好友, reward);
				response.addRewards(currencyHandle.packItem(reward));
				send(messageSource, response, Result.Success);

				TaskHandle.handle(playerCode, TaskType.统计型_好友_赠送_次数, num);
				TaskHandle.handle(playerCode, TaskType.统计型_好友_赠送_累计_达到, num);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			playerModule.unlock();
		}
	}

	private void send(MessageSource messageSource, FriendMessage.FriendPointGiveResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}